package com.wish233.wiliwilivideo.canal;

import com.wish233.wiliwilivideo.domain.po.Comment;
import com.wish233.wiliwilivideo.mq.MqProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import javax.annotation.Resource;

import static com.wish233.wiliwilicommon.constants.RedisConstants.COMMENT_CHILD_VIDEO_KEY;
import static com.wish233.wiliwilicommon.constants.RedisConstants.COMMENT_ROOT_VIDEO_KEY;
import static com.wish233.wiliwilivideo.utils.RedisUtils.calculateHotScore;

/**
 * 获取canal监听mysql的binlog的数据
 *
 * @author WHH
 */
// 指定要监听的表
@CanalTable("comment")
//@Component
@Slf4j
public class CommentInsertHandler implements EntryHandler<Comment> {


    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private MqProducer mqProducer;

    // 监听新增数据，插入redis
    @Override
    public void insert(Comment comment) {
        log.info("监听新增数据{}，插入redis", comment);
        mqProducer.sendCommentMessage(comment);
    }


    // 监听删除数据，删除本地缓存，删除redis数据
    @Override
    public void delete(Comment comment) {
        log.info("监听删除数据，删除本地缓存，删除redis数据", comment);
    }

    @Override
    public void update(Comment before, Comment after) {
        log.info("监听删除/修改数据，删除本地缓存，删除redis数据", after);
        mqProducer.sendDeleteComment(before);
    }
}